package com.mollytask.superadmin;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.mollytask.R;
import com.mollytask.base.BaseDetailActivity;
import com.mollytask.bean.CheckRecord;
import com.mollytask.bean.DataFill;
import com.mollytask.bean.Institution;
import com.mollytask.bean.Message;
import com.mollytask.util.DialogUtils;

import butterknife.BindView;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class DataCheckDetailActivity extends BaseDetailActivity {

    @BindView(R.id.pass)
    Button mBtnPass;
    @BindView(R.id.not_pass)
    Button mBtnNotPass;
    private TextView mPositive;

    private EditText mCheckerName;
    private EditText mCheckMessage;


    @Override
    public int setLayout() {
        return R.layout.activity_data_check_detail;
    }

    @Override
    public void receiveData() {
        mDataFill = (DataFill) getIntent().getSerializableExtra("dataFillResult");
        mInstitution = (Institution) getIntent().getSerializableExtra("institution");
    }

    @Override
    public void init() {

        mBtnPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessageDialog(true);
            }
        });

        mBtnNotPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessageDialog(false);

            }
        });
    }


    public void showMessageDialog(boolean isPass) {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .title("审核意见")
                .customView(R.layout.view_check, true)
                .canceledOnTouchOutside(false)
                .show();
        View view = dialog.getView();
        mPositive = (TextView) view.findViewById(R.id.positive);

        mCheckerName = (EditText) view.findViewById(R.id.checker_name);
        mCheckMessage = (EditText) view.findViewById(R.id.check_message);
        mPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                boolean cancle = false;

                if (isPass) {
                    if (TextUtils.isEmpty(mCheckerName.getText().toString())) {
                        cancle = true;
                        DialogUtils.showToast(DataCheckDetailActivity.this, "请输入统计员姓名");
                    }

                } else {
                    if (TextUtils.isEmpty(mCheckerName.getText().toString())) {
                        cancle = true;
                        DialogUtils.showToast(DataCheckDetailActivity.this, "请输入统计员姓名");
                    } else if (TextUtils.isEmpty(mCheckMessage.getText().toString())) {
                        cancle = true;
                        DialogUtils.showToast(DataCheckDetailActivity.this, "请输入审核意见");
                    }
                }

                if (!cancle) {
                    if (isPass) {
                        DataFill dataFill = new DataFill();
                        dataFill.setValue("status", 1);
                        dataFill.update(mDataFill.getObjectId(), new UpdateListener() {
                            @Override
                            public void done(BmobException e) {

                            }
                        });
                    } else {
                        DataFill dataFill = new DataFill();
                        dataFill.setStatus(2);
                        dataFill.update(mDataFill.getObjectId(), new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                            }
                        });
                    }

                    CheckRecord checkRecord = new CheckRecord();
                    checkRecord.setFillId(mDataFill.getmFillId());
                    checkRecord.setCheckResult(isPass ? "通过" : "未通过");
                    checkRecord.setCheckName(mCheckerName.getText().toString());
                    checkRecord.setCheckMessage(mCheckMessage.getText().toString());
                    checkRecord.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            DialogUtils.showToast(DataCheckDetailActivity.this, "审核完成");
                            Message message = new Message();
                            message.setmFromCode(mDataFill.getmSuperCode());
                            message.setmToCode(mInstitution.getmInCode());
                            message.setType(1);
                            message.setmContent("已审核您提交的" + mDataFill.getmFillId().substring(9, 13) + "年第" + mDataFill.getmFillId().substring(13, 14) + "季度的数据,结果为" + (isPass ? "通过" : "不通过") + "审核意见为" + mCheckMessage.getText().toString());
                            message.save(new SaveListener<String>() {
                                @Override
                                public void done(String s, BmobException e) {

                                }
                            });

                        }
                    });
                }
                finish();
            }
        });

    }
}
